package io.renren.modules.alarm.service.impl;

import com.alibaba.fastjson.JSON;
import io.renren.modules.alarm.dao.AirQualityDataDao;
import io.renren.modules.alarm.dao.AlarmListDao;
import io.renren.modules.alarm.dao.AlarmSensorDao;
import io.renren.modules.alarm.dao.AlarmTacticsDao;
import io.renren.modules.alarm.entity.*;
import io.renren.modules.collecting.dao.CollectingDeviceDao;
import io.renren.modules.collecting.entity.CollectingDeviceEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Component
public class AlarmListUtil implements ApplicationContextAware {
    
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }


    /**
     * 1、根据设备id获取最新的一条数据
     * 2、判断每个值是否超过限定的范围
     * 3、统计超过范围的数据个数，判断优良差
     * 4、将有数据超限的这条数据存到报警列表库中
     * 5、报警状态，暂定
     */

    //参数是报警策略id
    public void getAlarm(Integer id) {

        //@Scheduled注解方式级别高于资源注入级别，需要重写setApplicationContext方法，重新注入bean
        AlarmListDao alarmListDao = (AlarmListDao) this.getBean("alarmListDao");
        AirQualityDataDao airQualityDataDao = (AirQualityDataDao) this.getBean("airQualityDataDao");
        AlarmSensorDao alarmSensorDao = (AlarmSensorDao) this.getBean("alarmSensorDao");
        AlarmTacticsDao alarmTacticsDao = (AlarmTacticsDao) this.getBean("alarmTacticsDao");
        CollectingDeviceDao collectingDeviceDao = (CollectingDeviceDao) this.getBean("collectingDeviceDao");

        //获取报警策略详情
        AlarmTacticsEntity alarmTactics = alarmTacticsDao.get(id);
        //获取关联的传感器集合
        List<AlarmTacticsSensorEntity> alarmTacticsSensorList = alarmTacticsDao.getSensorList(id);
        //设备id集合
        List<Integer> deviceIds = alarmTacticsSensorList.stream().map(AlarmTacticsSensorEntity::getDeviceId).collect(Collectors.toList());
        //传感器id集合
        List<Integer> sensorIds = alarmTacticsSensorList.stream().map(AlarmTacticsSensorEntity::getSensorId).collect(Collectors.toList());
        //获取传感器限值集合
        List<AlarmSensorEntity> sensorList = alarmSensorDao.getListByIds(sensorIds);

        //应该只选一个
        //设备id
        Integer deviceId = deviceIds.get(0);
        //获取设备详情
        CollectingDeviceEntity device = collectingDeviceDao.get(deviceId);

        //定义报警数据个数
        int sum = 0;
        //获取采集数据
        AirQualityDataEntity airQualityData = airQualityDataDao.get(deviceId.toString());
        //报警数据集合
        List<AlarmValueEntity> alarmValueList = new ArrayList<>();

        if (null != airQualityData && !"".equals(airQualityData)) {
            //循环判断有几项数据超出范围
            for (AlarmSensorEntity sensor : sensorList) {
                //获取传感器编码
                String sensorCode = sensor.getSensorCode();
                //获取传感器名称
                String sensorName = sensor.getSensorName();

                if ("jcqdz1".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz1()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz1());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz1());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz2".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz2()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz2());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz2());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz3".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz3()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz3());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz3());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz4".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz4()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz4());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz4());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz5".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz5()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz5());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz5());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz6".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz6()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz6());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz6());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz7".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz7()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz7());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz7());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz8".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz8()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz8());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz8());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz9".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz9()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz9());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz9());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz10".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz10()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz10());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz10());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz11".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz11()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz11());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz11());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz12".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz12()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz12());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz12());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz13".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz13()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz13());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz13());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz14".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz14()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz14());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz14());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
                if ("jcqdz15".equals(sensorCode)) {
                    if (null != airQualityData.getJcqdz15()) {
                        //获取传感器报警上下限
                        AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                        String restrictStart = sensor.getRestrictStart();//限值起点
                        String restrictEnd = sensor.getRestrictEnd();//限值终点

                        BigDecimal d2 = new BigDecimal(restrictStart);//限值起点
                        BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                        BigDecimal d3 = new BigDecimal(restrictEnd);//限值终点
                        BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                        BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz15());//采集数据

                        //若超出限值范围则记录
                        if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                            sum++;
                            AlarmValueEntity valueEntity = new AlarmValueEntity();
                            valueEntity.setSensorCode(sensorCode);
                            valueEntity.setSensorName(sensorName);
                            valueEntity.setRestrictStart(restrictStart);
                            valueEntity.setRestrictEnd(restrictEnd);
                            valueEntity.setAlarmValue(airQualityData.getJcqdz15());
                            alarmValueList.add(valueEntity);
                        }
                    }
                }
            }
        }

        //判断超出范围的数据个数是否超出报警策略定义的规则
        if (new BigDecimal(sum).compareTo(new BigDecimal(alarmTactics.getRule())) == 1) {
            AlarmListEntity alarmList = new AlarmListEntity();
            alarmList.setTacticsId(alarmTactics.getId());//报警id
            alarmList.setTacticsLevel(alarmTactics.getLevel());//报警等级
            alarmList.setTacticsDetails(alarmTactics.getDetails());//报警内容
            alarmList.setSpaceId(device.getSpaceBind());//空间id
            alarmList.setDeviceId(deviceId);//设备id
            //alarmList.setSensorId();//传感器id
            alarmList.setAlarmValue(JSON.toJSONString(alarmValueList));//报警值
            alarmList.setTacticsState("报警");//报警状态
            alarmList.setDataTime(airQualityData.getCreateTime());//推送时间
            alarmList.setTacticsTime(new Date());//报警时间
            alarmListDao.add(alarmList);
        }
    }


    /**
     * 综合看板
     * 获取健康值和舒适度
     */
    public String getHealthOrComfort(String healthOrComfort, String spaceName) {

        //@Scheduled注解方式级别高于资源注入级别，需要重写setApplicationContext方法，重新注入bean
        AlarmListDao alarmListDao = (AlarmListDao) this.getBean("alarmListDao");
        AirQualityDataDao airQualityDataDao = (AirQualityDataDao) this.getBean("airQualityDataDao");
        AlarmSensorDao alarmSensorDao = (AlarmSensorDao) this.getBean("alarmSensorDao");
        AlarmTacticsDao alarmTacticsDao = (AlarmTacticsDao) this.getBean("alarmTacticsDao");
        CollectingDeviceDao collectingDeviceDao = (CollectingDeviceDao) this.getBean("collectingDeviceDao");


        //获取报警策略详情
        AlarmTacticsEntity alarmTactics = alarmTacticsDao.getHealthOrComfort(healthOrComfort);

        if (null == alarmTactics || "".equals(alarmTactics)) {
            return "";
        }

        Integer id = alarmTactics.getId();

        List<AlarmTacticsSensorEntity> alarmTacticsSensorList = alarmTacticsDao.getSensorList(id);
        //获取设备id
        Integer deviceId = alarmTacticsSensorList.get(0).getDeviceId();

        //根据空间名称获取设备id，并判断设备下是否有采集数据
        if (null != spaceName && !"".equals(spaceName)) {
            List<Integer> deviceIds = collectingDeviceDao.getDeviceIds(spaceName);
            if (!deviceIds.contains(deviceId)) {
                return null;
            }
        }

        CollectingDeviceEntity device = collectingDeviceDao.get(deviceId);

        //获取采集数据
        AirQualityDataEntity airQualityData = airQualityDataDao.get(deviceId.toString());
        if (null == airQualityData || "".equals(airQualityData)) {
            return "";
        }
        //获取传感器限值集合
        List<AlarmSensorEntity> sensorList = alarmSensorDao.getList(deviceId);

        //定义报警数据个数
        int sum = 0;
        //定于报警数据
        List<Map<String, String>> data = new ArrayList<>();

        //循环判断有几项数据超出范围
        for (AlarmSensorEntity sensor : sensorList) {
            //获取传感器编码
            String sensorCode = sensor.getSensorCode();

            if ("jcqdz1".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz1()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz1());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz1", airQualityData.getJcqdz1());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz1", null);
                    data.add(map);
                }
            }
            if ("jcqdz2".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz2()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz2());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz2", airQualityData.getJcqdz2());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz2", null);
                    data.add(map);
                }
            }
            if ("jcqdz3".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz3()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz3());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz3", airQualityData.getJcqdz3());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz3", null);
                    data.add(map);
                }
            }
            if ("jcqdz4".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz4()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz4());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz4", airQualityData.getJcqdz4());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz4", null);
                    data.add(map);
                }
            }
            if ("jcqdz5".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz5()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz5());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz5", airQualityData.getJcqdz5());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz5", null);
                    data.add(map);
                }
            }
            if ("jcqdz6".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz6()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz6());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz6", airQualityData.getJcqdz6());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz6", null);
                    data.add(map);
                }
            }
            if ("jcqdz7".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz7()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz7());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz7", airQualityData.getJcqdz7());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz7", null);
                    data.add(map);
                }
            }
            if ("jcqdz8".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz8()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz8());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz8", airQualityData.getJcqdz8());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz8", null);
                    data.add(map);
                }
            }
            if ("jcqdz9".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz9()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz9());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz9", airQualityData.getJcqdz9());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz9", null);
                    data.add(map);
                }
            }
            if ("jcqdz10".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz10()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz10());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz10", airQualityData.getJcqdz10());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz3", null);
                    data.add(map);
                }
            }
            if ("jcqdz11".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz11()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz11());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz11", airQualityData.getJcqdz11());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz11", null);
                    data.add(map);
                }
            }
            if ("jcqdz12".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz12()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz12());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz12", airQualityData.getJcqdz12());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz12", null);
                    data.add(map);
                }
            }
            if ("jcqdz13".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz13()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz13());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz13", airQualityData.getJcqdz13());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz13", null);
                    data.add(map);
                }
            }
            if ("jcqdz14".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz14()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz14());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz14", airQualityData.getJcqdz14());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz14", null);
                    data.add(map);
                }
            }
            if ("jcqdz15".equals(sensorCode)) {
                if (null != airQualityData.getJcqdz15()) {
                    //获取传感器报警上下限
                    AlarmTacticsSensorEntity alarmTacticsSensor = alarmTacticsDao.getSensor(id, sensor.getId());

                    BigDecimal d2 = new BigDecimal(sensor.getRestrictStart());//限值起点
                    BigDecimal d22 = d2.subtract(d2.multiply(new BigDecimal(alarmTacticsSensor.getUpper())));//报警下限

                    BigDecimal d3 = new BigDecimal(sensor.getRestrictEnd());//限值终点
                    BigDecimal d33 = d3.add(d3.multiply(new BigDecimal(alarmTacticsSensor.getLowe())));//报警上限

                    BigDecimal d1 = new BigDecimal(airQualityData.getJcqdz15());//采集数据

                    //若超出限值范围则记录
                    if (d22.compareTo(d1) == 1 || d1.compareTo(d33) == 1) {
                        sum++;
                        Map<String, String> map = new HashMap<>();
                        map.put("jcqdz15", airQualityData.getJcqdz15());
                        data.add(map);
                    }
                } else {
                    sum++;
                    Map<String, String> map = new HashMap<>();
                    map.put("jcqdz15", null);
                    data.add(map);
                }
            }
        }

        //判断超出范围的数据个数是否超出报警策略定义的规则
        if (new BigDecimal(sum).compareTo(new BigDecimal(alarmTactics.getRule())) == 1) {
            return alarmTactics.getLevel();
        } else {
            return "优";
        }
    }

}
